package com.nari.customer.action;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.apache.struts2.json.JSONUtil;
import org.apache.struts2.json.annotations.JSON;

import com.nari.baseinfo.model.IBuilding;
import com.nari.baseinfo.model.ICommunity;
import com.nari.baseinfo.model.IHeatRoom;
import com.nari.baseinfo.model.IProject;
import com.nari.baseinfo.service.IBuildingService;
import com.nari.baseinfo.service.ICommunityService;
import com.nari.baseinfo.service.IHeatRoomService;
import com.nari.baseinfo.service.IProjectService;
import com.nari.basic.model.IUser;
import com.nari.basic.service.IUserService;
import com.nari.coreframe.action.struts2.JSONActionSupport;
import com.nari.coreframe.webframe.SysEnvironmentMgr;
import com.nari.customer.bo.UserComplaint;
import com.nari.customer.service.ICustomerComplaintService;
import com.nari.platform.model.PageCond;
import com.nari.platform.model.ResponseMsg;
import com.nari.platform.model.ResultWithPageCond;
import com.nari.platform.model.WebConstants;
import com.opensymphony.xwork2.Action;

public class CustomerComplaintsAction extends JSONActionSupport {
	private static final long serialVersionUID = 8609589032416905289L;
	private ICustomerComplaintService iCustomerComplaintService;
	private IProjectService iProjectService;
	private ICommunityService iCommunityService;
	private IBuildingService iBuildingService;
	private IHeatRoomService iHeatRoomService;
	private IUserService iUserService;
	// 楼栋信息查询
	private String buildingId;
	private String projectId;
	private String communityId;
	private List<UserComplaint> userComplaintList;
	private List<IProject> projectList;
	private List<ICommunity> communityList;
	private List<IBuilding> buildingList;
	private List<IUser> userList;
	private Map<String, String> pageProjectMap; // 所属项目id-projectName映射表
	private Map<String, String> pageCommunityMap; // 所属小区id-communityName映射表
	private Map<String, String> pageBuildingMap; // 所属小区id-communityName映射表
	private Map<String, String> pageUserMap; // 所属小区id-communityName映射表
	// 分页查询
	private int pageIndex = 1; // 默认查询第一页列表
	private PageCond pageCond;
	private int pageSize;
	private ResultWithPageCond<UserComplaint> userComplaintPage;
	// 热用户信息
	private UserComplaint userComplaint;
	private String complaintId;
	private IUser user;
	private IBuilding building;
	private String sysReply;

	public String queryForPage() throws Exception {
		// 所属项目列表
		this.projectList = this.iProjectService.queryAllProjectInOrder();
		if (this.projectList == null) {
			this.projectList = new ArrayList<IProject>();
		}
		// 所属小区
		if (this.projectId != null && !"".equals(projectId)) {
			this.communityList = this.iCommunityService
					.queryCommunitysByBelongings(null, null, null, projectId);
		} else {
			this.communityList = iCommunityService.queryEntityList();
		}
		// 所属楼栋
		if ((this.communityId != null && !"".equals(communityId))
				|| (this.projectId != null && !"".equals(projectId))) {
			this.buildingList = this.iBuildingService
					.queryBuildingByBelongings(projectId, communityId);
		} else {
			this.buildingList = iBuildingService.queryEntityList();
		}
		// 分页信息
		this.pageCond = new PageCond();
		int start = (this.pageIndex - 1) * this.pageCond.getLimit();
		this.pageCond.setStart(start);
		//
		if (communityId != null && communityId.length() > 0) {
			ICommunity co = iCommunityService.queryEntityById(communityId);
			projectId = co.getProjectID();
		}
		// 分页结果
		this.userComplaintPage = this.iCustomerComplaintService.queryForPage(
				buildingId, projectId, communityId, pageCond);
		// 计算结果集的页数
		this.pageSize = new BigDecimal(this.userComplaintPage.getPageCond()
				.getTotal())
				.divide(new BigDecimal(this.userComplaintPage.getPageCond()
						.getLimit()), RoundingMode.UP).intValue();
		if (pageSize == 0)
			pageSize = 1;
		pageBuildingMap = new HashMap<String, String>();
		for (IBuilding b : buildingList) {
			pageBuildingMap.put(b.getId(), b.getBuildingID());
		}
		pageUserMap = new HashMap<String, String>();
		userList = iUserService.queryEntityList();
		for (IUser user : userList) {
			pageUserMap.put(user.getId(), user.getUserID());
		}
		// 查询所属小区映射表
		this.pageCommunityMap = this.iCustomerComplaintService
				.queryReferCommunityMap(userComplaintPage.getList());
		return "SUCCESS";
	}

	public String forInsert() throws Exception {
		// 楼栋信息
		if (this.complaintId != null && !"".equals(complaintId)) {
			this.userComplaint = this.iCustomerComplaintService
					.queryEntityById(complaintId);
			this.projectId = this.building.getProjectID();
			this.communityId = this.building.getCommunityID();
		}
		// 所属项目列表
		this.projectList = this.iProjectService.queryAllProjectInOrder();
		if (this.projectList == null) {
			this.projectList = new ArrayList<IProject>();
		}
		// 所属小区列表
		if (this.projectId != null && !"".equals(projectId)) {
			this.communityList = this.iCommunityService
					.queryCommunitysByBelongings(null, null, null, projectId);
		} else {
			this.communityList = new ArrayList<ICommunity>();
		}
		userComplaint = fillBaseInfo(userComplaint);
		return "insertPage";
	}

	private UserComplaint fillBaseInfo(UserComplaint userComplaint)
			throws Exception {
		HttpSession session = SysEnvironmentMgr.getSessionInstance();
		IUser user = (IUser) session.getAttribute(WebConstants.USER_SESSION);
		if (userComplaint == null) {
			userComplaint = new UserComplaint();
		}
		userComplaint.setUserId(user.getId());
		userComplaint.setLoginName(user.getUserID());
		IHeatRoom room = iHeatRoomService.queryEntityById(user.getRoomId());
		if (room != null) {
			IBuilding building = iBuildingService.queryEntityById(room
					.getBuildingID());
			if (building != null) {
				userComplaint.setBuildingId(building.getId());
			}
			ICommunity comm = iCommunityService.queryEntityById(room
					.getCommunityID());
			if (comm != null) {
				userComplaint.setCommunityId(comm.getId());
			}
			IProject pro = iProjectService.queryEntityById(room.getProjectID());
			if (pro != null) {
				userComplaint.setProjectId(pro.getId());
			}
		}
		return userComplaint;
	}

	public String saveCustomerComplaint() throws Exception {
		userComplaint.setComplaintTime(new Date());
		userComplaint = fillBaseInfo(userComplaint);
		this.iCustomerComplaintService.saveOrUpdateUserComplaint(userComplaint);
		this.writeJsonStrToResponse(JSONUtil.serialize(ResponseMsg.getInstance(
				true, "")));
		return Action.SUCCESS;
	}

	public String deleteCustomerComplaint() throws Exception {
		this.iCustomerComplaintService.deleteObjectById(complaintId);
		this.writeJsonStrToResponse(JSONUtil.serialize(ResponseMsg.getInstance(
				true, "")));
		return Action.SUCCESS;
	}

	public String detail() throws Exception {
		if (this.complaintId != null && !"".equals(complaintId)) {
			this.userComplaint = this.iCustomerComplaintService
					.queryForDetail(complaintId);
		}
		return "detail";
	}

	public String forReply() throws Exception {
		if (this.complaintId != null && !"".equals(complaintId)) {
			this.userComplaint = this.iCustomerComplaintService
					.queryForDetail(complaintId);
		}
		return "reply";
	}

	public String replyCustomerComplaint() throws Exception {
		userComplaint = iCustomerComplaintService.queryEntityById(complaintId);
		HttpSession session = SysEnvironmentMgr.getSessionInstance();
		IUser user = (IUser) session.getAttribute(WebConstants.USER_SESSION);
		userComplaint.setReplyNo(user.getId());
		userComplaint.setReplyTime(new Date());
		userComplaint.setSysReply(sysReply);
		this.iCustomerComplaintService.updateObject(userComplaint);
		this.writeJsonStrToResponse(JSONUtil.serialize(ResponseMsg.getInstance(
				true, "")));
		return Action.SUCCESS;
	}

	@JSON(serialize=false)
	public ICustomerComplaintService getiCustomerComplaintService() {
		return iCustomerComplaintService;
	}

	public void setiCustomerComplaintService(
			ICustomerComplaintService iCustomerComplaintService) {
		this.iCustomerComplaintService = iCustomerComplaintService;
	}

	@JSON(serialize=false)
	public IProjectService getiProjectService() {
		return iProjectService;
	}

	public void setiProjectService(IProjectService iProjectService) {
		this.iProjectService = iProjectService;
	}

	@JSON(serialize=false)
	public ICommunityService getiCommunityService() {
		return iCommunityService;
	}

	public void setiCommunityService(ICommunityService iCommunityService) {
		this.iCommunityService = iCommunityService;
	}

	@JSON(serialize=false)
	public IBuildingService getiBuildingService() {
		return iBuildingService;
	}

	public void setiBuildingService(IBuildingService iBuildingService) {
		this.iBuildingService = iBuildingService;
	}

	public String getBuildingId() {
		return buildingId;
	}

	public void setBuildingId(String buildingId) {
		this.buildingId = buildingId;
	}

	public String getProjectId() {
		return projectId;
	}

	public void setProjectId(String projectId) {
		this.projectId = projectId;
	}

	public String getCommunityId() {
		return communityId;
	}

	public void setCommunityId(String communityId) {
		this.communityId = communityId;
	}

	public List<UserComplaint> getUserComplaintList() {
		return userComplaintList;
	}

	public void setUserComplaintList(List<UserComplaint> userComplaintList) {
		this.userComplaintList = userComplaintList;
	}

	public List<IProject> getProjectList() {
		return projectList;
	}

	public void setProjectList(List<IProject> projectList) {
		this.projectList = projectList;
	}

	public List<ICommunity> getCommunityList() {
		return communityList;
	}

	public void setCommunityList(List<ICommunity> communityList) {
		this.communityList = communityList;
	}

	public Map<String, String> getPageProjectMap() {
		return pageProjectMap;
	}

	public void setPageProjectMap(Map<String, String> pageProjectMap) {
		this.pageProjectMap = pageProjectMap;
	}

	public Map<String, String> getPageCommunityMap() {
		return pageCommunityMap;
	}

	public void setPageCommunityMap(Map<String, String> pageCommunityMap) {
		this.pageCommunityMap = pageCommunityMap;
	}

	public int getPageIndex() {
		return pageIndex;
	}

	public void setPageIndex(int pageIndex) {
		this.pageIndex = pageIndex;
	}

	public PageCond getPageCond() {
		return pageCond;
	}

	public void setPageCond(PageCond pageCond) {
		this.pageCond = pageCond;
	}

	public int getPageSize() {
		return pageSize;
	}

	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}

	public ResultWithPageCond<UserComplaint> getUserComplaintPage() {
		return userComplaintPage;
	}

	public void setUserComplaintPage(
			ResultWithPageCond<UserComplaint> userComplaintPage) {
		this.userComplaintPage = userComplaintPage;
	}

	public UserComplaint getUserComplaint() {
		return userComplaint;
	}

	public void setUserComplaint(UserComplaint userComplaint) {
		this.userComplaint = userComplaint;
	}

	public IUser getUser() {
		return user;
	}

	public void setUser(IUser user) {
		this.user = user;
	}

	@JSON(serialize=false)
	public IHeatRoomService getiHeatRoomService() {
		return iHeatRoomService;
	}

	public void setiHeatRoomService(IHeatRoomService iHeatRoomService) {
		this.iHeatRoomService = iHeatRoomService;
	}

	public String getComplaintId() {
		return complaintId;
	}

	public void setComplaintId(String complaintId) {
		this.complaintId = complaintId;
	}

	public IBuilding getBuilding() {
		return building;
	}

	public void setBuilding(IBuilding building) {
		this.building = building;
	}

	public List<IBuilding> getBuildingList() {
		return buildingList;
	}

	public void setBuildingList(List<IBuilding> buildingList) {
		this.buildingList = buildingList;
	}

	@JSON(serialize=false)
	public IUserService getiUserService() {
		return iUserService;
	}

	public void setiUserService(IUserService iUserService) {
		this.iUserService = iUserService;
	}

	public List<IUser> getUserList() {
		return userList;
	}

	public void setUserList(List<IUser> userList) {
		this.userList = userList;
	}

	public Map<String, String> getPageBuildingMap() {
		return pageBuildingMap;
	}

	public void setPageBuildingMap(Map<String, String> pageBuildingMap) {
		this.pageBuildingMap = pageBuildingMap;
	}

	public Map<String, String> getPageUserMap() {
		return pageUserMap;
	}

	public void setPageUserMap(Map<String, String> pageUserMap) {
		this.pageUserMap = pageUserMap;
	}

	public String getSysReply() {
		return sysReply;
	}

	public void setSysReply(String sysReply) {
		this.sysReply = sysReply;
	}

}